<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">

        请输入您的问题：<input type="text" v-model="question"  /><br/>
        答案：<div>{{answer}}</div>

    </div>
    
    <script src="./js/vue.js"></script>
    <script>
        let vm = new Vue({
            el:"#app",
            data:{
                question:"",
                answer:""                
            },
            watch:{
                question:function(newVal,oldVal){
                    // console.log("watch执行",newVal,oldVal);
                    let  xhr = new XMLHttpRequest();
                    xhr.open("get","./answer.php?question="+newVal);
                    xhr.onreadystatechange = ()=>{
                        if(xhr.readyState==4 && xhr.status==200){
                            this.answer = xhr.responseText;                            
                        }
                    }
                    xhr.send();                    
                }
            }
        });


    </script>
</body>
</html>